<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<html class="no-js">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <title>软件管理平台</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="viewport" content="width=device-width">
    <link href="../statics/assets/bootstrap/css/bootstrap.min.css" rel='stylesheet'/>
    <link href="../statics/css/common/common.css" rel='stylesheet'/>
    <%--<script src="../statics/assets/jquery/jquery-3.1.0.js"></script>--%>
    <script src="../statics/assets/jquery/jquery-3.1.0.min.js"></script>
    <script src="../statics/assets/layui-layer-v2.4/layer.js"></script>
    <%--分页插件引入，使用方法参照官网：http://jqpaginator.keenwon.com/--%>
    <script src="../statics/assets/jqPaginator-1.2.0/jqPaginator.min.js"></script>

    <script type="text/javascript" language="javascript">
        var basePath = "<%=basePath%>";

        // 定义每页软件数量
        var pageSize = 3;

        // 定义全局软件分类id
        var global_categoryId;

        var currentPage = 1;

        $(document).ready(function(){

            // 定义初始化方法
            initFun = function () {

                // 查询软件分类
                querySoftwareCategory();

                // 查询软件，初始化时为查询第一页
                querySoftware(1);
            }

            /**
             * 查询软件分类
             * @author zhangbo 01084074 on 2016/8/31.
             */
            querySoftwareCategory = function () {
                $.ajax( {
                    url:basePath +　"/softwarecategory/get",
                    dataType:'json',
                    success:function(data) {
                        $.each(data, function(index, content) {
                            var li = '<li class="download-nav-item">' +
                                    '<a id="commonSW" class="download-nav-btn" href="javascript:getByCategoryId(' + content.id + ');">' + content.name + '</a> ' +
                                    '</li>';

                            $(".download-nav-content").append($(li));
                        });
                    },
                    error : function() {
                        alert("异常！");
                    }
                });
            }

            /**
             * 查询软件
             * @param pageNum     分页：第几页
             * @author zhangbo 01084074 on 2016/9/2.
             */
            querySoftware = function (pageNum) {
                var url = basePath +　"/softwarereview/getreview";
                var data = {
                    pageNum: pageNum,
                    categoryId: global_categoryId
                };

                $.ajax({
                    url: url,
                    data: data,
                    dataType: 'json',
                    success: function(data) {
                        if (data.status == "success") {
                            // 构造软件列表
                            createSoftwareList(data.items);

                            // 构造分页插件
                            createPagination(data.total);
                        } else {
                            alert("请求失败！")
                        }
                    },
                    error: function() {
                        alert("异常！");
                    }
                });
            }

            /**
             * 创建软件列表
             * @param data     软件对象数组
             * @author zhangbo 01084074 on 2016/9/2.
             */
            createSoftwareList = function (data) {
                $(".download-app-box").empty();
                $.each(data, function(index, content) {
                    var li = '<li class="app-item clearfix">' +
                            '<span class="app-img-box fl"> ' +
                            '<img class="app-img fl" src="' + basePath + content.icon + '" onclick="openDetail(' + content.id + ')"/> ' +
                            '</span> ' +
                            '<div class="app-text-box fl"> ' +
                            '<p class="app-name">' +
                            '<a href="javascript:openDetail(' + content.id + ');">' + content.name + '</a>' +
                            '</p> ' +
                            '<p class="app-text">' + content.description + '</p> ' +
                            '<p class="app-text">更新时间：' + content.updateTime + '</p>' +
                            '</div> ' +
                            '<a class="app-btn fl" href="javascript:openDetail(' + content.id + ');">详情</a> ' +
                            '</li>';

                    $(".download-app-box").append($(li));
                });
            };

            /**
             * 通过软件分类id查找软件
             * @param categoryId    软件分类id
             * @author zhangbo 01084074 on 2016/8/31.
             */
            getByCategoryId = function (categoryId) {
                // 赋值全局软件分类id
                global_categoryId = categoryId;

                // 点击分类，默认查询第一页数据
                querySoftware(1);
            }

            /**
             * 点击详情，弹出一个iframe层，进入详情页面
             * @param softwareId    软件id
             * @author zhangbo 01084074 on 2016/9/1.
             */
            openDetail = function (softwareId) {
                layer.open({
                    type: 2,
                    title: '软件详细信息',
                    maxmin: true,
                    shadeClose: true, //点击遮罩关闭层
                    area : ['850px' , '700px'],
                    content: '/software/getSoftware?id=' + softwareId
                });
            }

            /**
             * 创建翻页组件
             * @param total     软件总数量
             * @author zhangbo 01084074 on 2016/9/2.
             */
            createPagination = function (total) {
                var totalPages = Math.ceil(total / pageSize);
                $("#paginator").jqPaginator({
                    totalPages: totalPages,
                    visiblePages: 7,
                    currentPage: currentPage,
                    activeClass: "active",
                    onPageChange: function (num, type) {
                        // 若是翻页，则触发后台查询
                        if ( type== "change") {
                            currentPage = num;
                            querySoftware(num);
                        }
                    }
                });
            }

            /**
             * 点击详情，弹出一个iframe层，进入详情页面
             */
            openDetail = function (softwareId) {
                layer.open({
                    type: 2,
                    title: '软件详细信息',
                    maxmin: true,
                    shadeClose: false, //点击遮罩关闭层
                    area : ['800px' , '600px'],
                    content: 'reviewDetail.jsp?softwareId=' + softwareId
                });

            }

            // 初始化页面调用
            initFun();

        });

    </script>

</head>
<body>
<div class="main">
    <div id="download" class="content-box clearfix">
        <div class="download-nav fl">
            <h3 class="download-nav-title">软件分类</h3>
            <ul class="download-nav-content">
            </ul>
        </div>
        <div class="fr">
            <div class="download-content">
                <ul class="download-app-box">
                </ul>
            </div>
            <div style="text-align: center">
                <div id="paginator" class="pagination"></div>
            </div>
        </div>
    </div>
</div>
</body>
</html>